

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.KeyEvent;

public class Test {

    public static void main(String[] args) throws InterruptedException {

        //String friendNickName = "文件传输助手";
        String friendNickName = "好友名字";
        Thread.sleep(2000);
        searchMyFriendAndSend(friendNickName);
    }

    private static void searchMyFriendAndSend(String friendNickName) throws InterruptedException {
        // 创建Robot对象
        Robot robot = getRobot();
        Thread.sleep(2000);
        //打开微信 快捷键Ctrl+Alt+W
        assert robot != null;
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_ALT);
        robot.keyPress(KeyEvent.VK_W);
        //释放Ctrl按键
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_ALT);
        // 延迟
        robot.delay(1000);

        // 快捷键Ctrl + F 搜索指定的好友
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_F);
        robot.keyRelease(KeyEvent.VK_CONTROL);

        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable tText = new StringSelection(friendNickName);
        clip.setContents(tText, null);
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.delay(1000);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.delay(1000);

        // 发送消息
        sendMsg();
    }

    private static void sendMsg() throws InterruptedException {
        Image image1 = Toolkit.getDefaultToolkit().getImage("1001.jpg");
        Image image2 = Toolkit.getDefaultToolkit().getImage("1002.jpg");
        Image image3 = Toolkit.getDefaultToolkit().getImage("1003.jpg");
        Image image4 = Toolkit.getDefaultToolkit().getImage("1004.jpg");
        Image image5 = Toolkit.getDefaultToolkit().getImage("1005.jpg");
        Image image6 = Toolkit.getDefaultToolkit().getImage("1006.jpg");
        String[] mottoes = {
                "孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡",
                "孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡",
                "孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡",
                "孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡",
                "孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡",
                "孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡",
                "孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡",
                "孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡孤寡"

        };
        sendOneMsg(image1);
        sendOneMsg("您好，我是你朋友***给您预定的七夕青蛙");
        sendOneMsg(image6);

        Thread.sleep(2000);
        sendOneMsg(image2);
        for (String motto : mottoes) {
            sendOneMsg(motto);
        }
        Thread.sleep(2000);
        sendOneMsg(image4);
        Thread.sleep(2000);

        for (int i=0;i<5;i++){
            sendOneMsg(image5);
        }
        sendOneMsg("祝你明年脱单 !");
        sendOneMsg(image3);
        sendOneMsg("说明下：爷是代码自动发的哦[胜利]");
    }

    private static void sendOneMsg(String msg) {
        // 创建Robot对象
        Robot robot = getRobot();
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable tText = new StringSelection(msg);
        clip.setContents(tText, null);
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        //发送
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.delay(1000);
    }

    //sendOneMsg()函数重载
    private static void sendOneMsg(Image image) {
        // 创建Robot对象
        Robot robot = getRobot();
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        Images images = new Images(image);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(images, null);
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.delay(1000);
    }

    private static Robot getRobot() {
        // 创建Robot对象
        Robot robot = null;
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
        return robot;
    }

}
